package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.Sharep;
import com.typy.www.tpay.util.CheckUtil;
import com.typy.www.tpay.util.CountDownTimerUtils;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PreferencesUtil;
import com.typy.www.tpay.util.SharedPreferencesUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/6/11.
 */

public class RegisterActivity extends BaseBackActivity {


    @BindView(R.id.et_yaoqingma)
    AutoCompleteTextView etYaoqingma;
    @BindView(R.id.et_nicheng)
    AutoCompleteTextView etNicheng;
    @BindView(R.id.et_phonenumber)
    AutoCompleteTextView etPhonenumber;
    @BindView(R.id.et_password)
    AutoCompleteTextView etPassword;
    @BindView(R.id.et_paypassword)
    AutoCompleteTextView etPaypassword;
    @BindView(R.id.et_yanzhengma)
    AutoCompleteTextView etYanzhengma;
    @BindView(R.id.btn_yanzhengma)
    TextView btnYanzhengma;
    @BindView(R.id.btn_queding)
    TextView btnQueding;

    public static void show(Context context) {
        IntentUtil.intent2Activity(context, RegisterActivity.class);
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("注册");

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public int getStatusBarColor() {
        return R.color.statusbar_blue45ACF9;
    }

    @OnClick({R.id.btn_yanzhengma, R.id.btn_queding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_yanzhengma:
                if (CheckUtil.checkPhoneTrue(getActivity(), etPhonenumber)) {
                    CountDownTimerUtils.getInstant(btnYanzhengma, 60000, 1000).start();
                    doSendMSN();
                }
                break;
            case R.id.btn_queding:
                if (CheckUtil.checkText(this, etNicheng, etPhonenumber, etPassword, etPaypassword, etYanzhengma)) {
                    if (CheckUtil.checkPhoneTrue(getActivity(), etPhonenumber)&&CheckUtil.checkText(getActivity(),"请输入6位支付密码",6,etPaypassword))
                        doRegister();
                }
                break;
        }
    }

    private void doRegister() {
        ParamsUtil params = new ParamsUtil();
        params.put("fdTuiJianId", etYaoqingma);//邀请码
        params.put("fdNickName", etNicheng);//昵称
        params.put("fdCode", etPhonenumber);//手机号
        params.putMd5("fdPwd", etPassword);//密码，Md5加密后,都是小写的
        params.putMd5("fdPayPwd", etPaypassword);//支付密码，Md5加密后,都是小写的
        params.put("fdValidCode", etYanzhengma);//验证码
        HTTPUtils.post( getActivity(),Constance.getRegisterURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), ret.getMsg(), true);
                PreferencesUtil.getValue( etPhonenumber.getText().toString());
                EventBus.getDefault().post(new MessageEvent(MessageAction.REFRESHLOGINNAME));
                finish();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void doSendMSN() {
        Map<String, String> params = new HashMap<>();
        params.put("fdCode", etPhonenumber.getText().toString());//手机号
        params.put("fdType", "0");//fdType 0表示注册验证码 1表示忘记密码 2表示修改支付密码
        HTTPUtils.post(getActivity(), Constance.getGetcodeURL(), params, new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), "验证码发送成功！", true);
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

}
